home *** CD-ROM | disk | FTP | other *** search
/ Atari Mega Archive 1 / Atari Mega Archive - Volume 1.iso / gnu / progutil / iostream.zoo / src / makebuf.cc < prev    next >
Encoding:
C/C++ Source or Header  |  1991-09-22  |  2.3 KB  |  81 lines

  1. /*
  2.  * Copyright (c) 1990 The Regents of the University of California.
  3.  * All rights reserved.
  4.  *
  5.  * Redistribution and use in source and binary forms are permitted
  6.  * provided that the above copyright notice and this paragraph are
  7.  * duplicated in all such forms and that any documentation,
  8.  * advertising materials, and other materials related to such
  9.  * distribution and use acknowledge that the software was developed
  10.  * by the University of California, Berkeley.  The name of the
  11.  * University may not be used to endorse or promote products derived
  12.  * from this software without specific prior written permission.
  13.  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
  14.  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
  15.  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  16.  */
  17.  
  18. // Modified for GNU iostream by Per Bothner 1991.
  19.  
  20. #if defined(LIBC_SCCS) && !defined(lint)
  21. static char sccsid[] = "%W% (Berkeley) %G%";
  22. #endif /* LIBC_SCCS and not lint */
  23.  
  24. #include "ioprivat.h"
  25. #include "fstream.h"
  26. #include <sys/types.h>
  27. #include <sys/stat.h>
  28.  
  29. /*
  30.  * Allocate a file buffer, or switch to unbuffered I/O.
  31.  * Per the ANSI C standard, ALL tty devices default to line buffered.
  32.  *
  33.  * As a side effect, we set __SOPT or __SNPT (en/dis-able fseek
  34.  * optimisation) right after the _fstat() that finds the buffer size.
  35.  */
  36. int filebuf::doallocate()
  37. {
  38.     if (unbuffered()) {
  39.     setb(_fb._shortbuf, _fb._shortbuf+1, 0);
  40.     return 1;
  41.     }
  42.     register size_t size, couldbetty;
  43.     register void *p;
  44.     struct stat st;
  45.  
  46.     if (fd() < 0 || _fstat(fd(), &st) < 0) {
  47.         couldbetty = 0;
  48.         size = BUFSIZ;
  49. #if 0
  50.         /* do not try to optimise fseek() */
  51.         fp->_flags |= __SNPT;
  52. #endif
  53.     } else {
  54.         couldbetty = (st.st_mode & S_IFMT) == S_IFCHR;
  55.         size = st.st_blksize <= 0 ? BUFSIZ : st.st_blksize;
  56. #if 0
  57.         /*
  58.          * Optimise fseek() only if it is a regular file.
  59.          * (The test for __sseek is mainly paranoia.)
  60.          */
  61.         if ((st.st_mode & S_IFMT) == S_IFREG &&
  62.             fp->_seek == __sseek) {
  63.             fp->_flags |= __SOPT;
  64.             fp->_blksize = st.st_blksize;
  65.         } else
  66.             fp->_flags |= __SNPT;
  67. #endif
  68.     }
  69.     if ((p = malloc(size)) == NULL) {
  70.         unbuffered(1);
  71. //        fp->_bf._base = fp->_p = fp->_nbuf;
  72. //        fp->_bf._size = 1;
  73.         return EOF;
  74.     } else {
  75.         setb(p, p+size, 1);
  76.         if (couldbetty && _isatty(fd()))
  77.             _flags |= _S_LINE_BUF;
  78.         return 1;
  79.     }
  80. }
  81.